package mdm.led.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mdm.led.common.AjaxResponse;
import mdm.led.common.Constant;
import mdm.led.entity.ExternalUser;
import mdm.led.entity.User;
import mdm.led.service.ExternalUserService;
import mdm.led.service.UserService;

/**
 *
 * @author :Monarch丿He
 * @date :2018年12月11日
 * @explain :
 */
@Controller
@RequestMapping("externalWebUser")
@Api(description = "外部联系人管理WEB")
public class ExternalUserWebController {

	private Logger logger = Logger.getLogger(ExternalUserWebController.class);
	@Autowired
	private ExternalUserService externalUserService;
	@Autowired
	private UserService userService;

	@ApiOperation(value = "添加外部联系人", notes = "返回值:<br>100 成功 <br>101:失败 <br>102:账号已存在")
	@RequestMapping(value = "addExternalUser", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResponse addExternalUser(@RequestBody ExternalUser user) {
		logger.info("\n======== 添加外部联系人 ========\n");
		boolean flag = false;
		try {
			// 判断账号是否存在
			ExternalUser findExternaUserByAccount = externalUserService.findExternaUserByAccount(user.getAccount());
			if (findExternaUserByAccount != null) {
				return AjaxResponse.reponseBody(102, Constant.SUCCESS_MSG, "账号已存在!");
			}
			// 添加外部联系人
			Integer addExternalUser = externalUserService.addExternalUser(user);
			if (addExternalUser > 0) {
				flag = true;
			}
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		}
		logger.info("\n======== 结果 : " + (flag == true ? "成功" : "失败") + " ========\n");
		if (flag) {
			return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, "添加成功");
		} else {
			return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, "添加失败");
		}
	}
	
	@ApiOperation(value="根据部门ID 查询部门人",notes = "入参: <br> departmentID : 部门ID")
	@RequestMapping(value = "findDepartmentUserByID", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResponse findDepartmentUserByID(@RequestParam("departmentID")Integer departmentID){
		logger.info("\n======== 根据部门ID 查询部门人  ========\n");
		boolean flag = false;
		// 需要返回的 数据集合
		List<User> users = null;
		try {
			users = userService.findDepartmentUserByID(departmentID, "民警");
			flag = true;
		} catch (Exception e) {
			flag = false;
			logger.info(e);
		}
		logger.info("\n======== 结果 : " + (flag == true ? "成功" : "失败") + " ========\n");
		logger.info("\n======== 结果值 : \n" + JSON.toJSONString(users) + " ========\n");
		if (flag) {
			return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, users);
		} else {
			return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, "异常");
		}
	}

	@ApiOperation(value = "修改外部联系人", notes = "返回值:<br>100 成功 <br>101:失败 <br>102:账号已存在")
	@RequestMapping(value = "updateExternalUser", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResponse updateExternalUser(@RequestBody ExternalUser user) {
		logger.info("\n======== 修改外部联系人  ========\n");
		boolean flag = false;
		try {
			// 修改外部联系人
			Integer updateExternalUser = externalUserService.updateExternalUser(user);
			if (updateExternalUser > 0) {
				flag = true;
			}
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		}
		logger.info("\n======== 结果 : " + (flag == true ? "成功" : "失败") + " ========\n");
		if (flag) {
			return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, "修改成功");
		} else {
			return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, "修改失败");
		}
	}

	@ApiOperation("查询外部联系人列表")
	@RequestMapping(value = "findExternaUser", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResponse findExternaUser(@RequestBody ExternalUser user, @RequestParam("pageSize") Integer pageSize,
			@RequestParam("currPage") Integer currPage) {
		logger.info("\n======== 询外部联系人列表询外部联系人列表  ========\n");
		boolean flag = false;
		// 需要返回的 map 数据集合
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			// 开启分页
			Page<ExternalUser> page = PageHelper.startPage(currPage, pageSize);
			// 查询外部联系人列表
			List<ExternalUser> findExternaUser = externalUserService.findExternaUser(user);
			// 数据
			data.put("users", findExternaUser);
			// 当前页
			data.put("currPage", page.getPageNum());
			// 页总数
			data.put("pageCount", page.getPages());
			// 页大小
			data.put("pageSize", page.getPageSize());
			// 总条数
			data.put("totalCount", page.getTotal());
			flag = true;
		} catch (Exception e) {
			flag = false;
			logger.info(e);
		}
		logger.info("\n======== 结果 : " + (flag == true ? "成功" : "失败") + " ========\n");
		logger.info("\n======== 结果值 : \n" + JSON.toJSONString(data) + " ========\n");
		if (flag) {
			return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, data);
		} else {
			return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, "异常");
		}
	}

	@ApiOperation("根据id查询外部联系人")
	@RequestMapping(value = "findExternaUserById", method = RequestMethod.GET)
	@ResponseBody
	public AjaxResponse findExternaUserById(@RequestParam("userid") Integer id) {
		logger.info("\n======== 询外部联系人列表询外部联系人列表  ========\n");
		boolean flag = false;
		ExternalUser externaUser = null;
		try {
			// 根据id查询外部联系人
			externaUser = externalUserService.findExternaUserById(id);
			flag = true;
		} catch (Exception e) {
			flag = false;
			logger.info(e);
		}
		logger.info("\n======== 结果 : " + (flag == true ? "成功" : "失败") + " ========\n");
		logger.info("\n======== 结果值 : \n" + JSON.toJSONString(externaUser) + " ========\n");
		if (flag) {
			return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, externaUser);
		} else {
			return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, "异常");
		}
	}

}
